﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using IDAL;

namespace DAL
{
    public class VideoInVideoPage : IVideoInVideoPage
    {
        DataMode.DataModeDataContext ctx = new DataMode.DataModeDataContext();
        public DataMode.video GetVideoInfoByVideoID(int videoID)
        {
            try
            {
                return ctx.video.Single(b => b.video_id == videoID);
            }
            catch (InvalidOperationException)
            {
                return null;
            }
        }

        public DataMode.video_album GetAlbumInfoByAlbumID(int albumID)
        {
            try
            {
                return ctx.video_album.Single(b => b.video_album_id == albumID);
            }
            catch (InvalidOperationException)
            {
                return null;
            }
        }

        public IOrderedQueryable GetVideosByAlbumID(int albumID)
        {
            return (IOrderedQueryable)(from video in ctx.video
                   where video.video_album_id == albumID
                   orderby video.load_time descending
                   select video).Take(8);
        }

        public int GetVideosNumByAlbumID(int albumID)
        {
            return (from video in ctx.video
                    where video.video_album_id == albumID
                    select video).Count();
        }
    }
}
